////
//
/// \file   ./src/internal_name_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/internal_name_node.hpp>

#include <cassert>
#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/compound_name_node.hpp>
#include <inc/parser/label_node.hpp>
#include <inc/parser/ws_parser.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;
using std::string;

/// \brief  Default constructor
/// \author Chandler Carruth
/// \date   2008.05.16
///
/// This simply allocates a WSParser and keeps it in the internal scoped_ptr.

InternalNameNode::InternalNameNode()
    : ws_parser( new WSParser )
{}

/// \brief  Protected injection constructor
/// \author Chandler Carruth
/// \date   2008.05.16
///
/// This constructor provides a dependency injection point for subclasses and
/// mocks.

InternalNameNode::InternalNameNode( WSParser *p )
    : ws_parser( p )
{
  assert( ws_parser && "Null pointer injected!" );
}

/// \brief  Parse an internal name node
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This builds an internal name parse node for use within nested name
/// resolution chains. It parses out either an explicit label for the name or
/// enforces grouping parentheses before handing off the parse to a compound
/// name construct.

bool
InternalNameNode::parse( const iterator &first,
                         const iterator &last,
                         iterator &last_parsed )
{
  assert( get_text().empty() && !node_ptr
          && "This node was parsed more than once!" );

  //  We have a simple look-ahead heuristic for compound names: they must
  //  always be parenthetically grouped when encountered within some other name
  //  resolution construct. This allows us to simply test for an opening
  //  parenthesis and completely decide whether to parse a compound name or
  //  a label.
  if( *first != '(' )
  {
    //  Attempt to parse a label node.
    boost::scoped_ptr<ParseTreeNode> label_node( new LabelNode );
    if( label_node->parse( first, last, last_parsed ) )
    {
      set_text( label_node->get_text() );
      node_ptr.swap( label_node );
      return true;
    }
    //  Everything else must be grouped
    return false;
  }

  //  Step over '('.
  iterator current = first + 1;
  
  //  Consume and step over any whitespace.
  ws_parser->parse_ws( current, last, last_parsed );
  current = last_parsed;

  boost::scoped_ptr<ParseTreeNode> compound_name( new CompoundNameNode );
  if( compound_name->parse( current, last, last_parsed ) )
  {
    current = last_parsed;

    //  Consuming and step over any more whitespace.
    ws_parser->parse_ws( current, last, last_parsed );
    current = last_parsed;

    //  Match the closing parenthesis.
    if( current < last && *current == ')' )
    {
      ++current;
      set_text( string( first, current ) );
      node_ptr.swap( compound_name );
      last_parsed = current;
      return true;
    }
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief  Convenience getter for a Label sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

LabelNode* InternalNameNode::get_label() const
{
  return dynamic_cast<LabelNode*>( node_ptr.get() );
}

/// \brief  Convenience getter for a CompoundName sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

CompoundNameNode* InternalNameNode::get_compound_name() const
{
  return dynamic_cast<CompoundNameNode*>( node_ptr.get() );
}

} //  end parser namespace
} //  end inc namespace
